package com.itboy.mall.pms.skuimages.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itboy.mall.pms.skuimages.domain.MallSkuImages;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.skuimages.vo.MallSkuImagesVo;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesQueryBo;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesAddBo;
import com.itboy.mall.pms.skuimages.bo.MallSkuImagesEditBo;
import com.itboy.mall.pms.skuimages.service.IMallSkuImagesService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * sku图片Controller
 *
 * @author itboy
 * @date 2021-08-07
 */
@Validated
@Api(value = "sku图片控制器", tags = {"sku图片管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/skuImages")
public class MallSkuImagesController extends BaseController {

    private final IMallSkuImagesService iMallSkuImagesService;

    /**
     * 查询sku图片列表
     */
    @ApiOperation("查询sku图片列表")
    @PreAuthorize("@ss.hasPermi('mall:skuImages:list')")
    @GetMapping("/list")
    public TableDataInfo<MallSkuImagesVo> list(@Validated MallSkuImagesQueryBo bo) {
        return iMallSkuImagesService.queryPageList(bo);
    }

    /**
     * 导出sku图片列表
     */
    @ApiOperation("导出sku图片列表")
    @PreAuthorize("@ss.hasPermi('mall:skuImages:export')")
    @Log(title = "sku图片", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallSkuImagesVo> export(@Validated MallSkuImagesQueryBo bo) {
        List<MallSkuImagesVo> list = iMallSkuImagesService.queryList(bo);
        ExcelUtil<MallSkuImagesVo> util = new ExcelUtil<MallSkuImagesVo>(MallSkuImagesVo.class);
        return util.exportExcel(list, "sku图片");
    }

    /**
	 * 获取sku图片详细信息
	 */
	@ApiOperation("获取sku图片详细信息")
	@PreAuthorize("@ss.hasPermi('mall:skuImages:query')")
	@GetMapping("/{id}")
	public AjaxResult<MallSkuImagesVo> getInfo(@NotNull(message = "主键不能为空")
											   @PathVariable("id") Long id) {
		return AjaxResult.success(iMallSkuImagesService.queryById(id));
	}

	/**
	 * 获取sku图片详细信息
	 */
	@ApiOperation("获取sku图片详细信息")
	@GetMapping("/querySkuImagesBySkuId/{skuId}")
	public AjaxResult<List<MallSkuImages>> querySkuImagesBySkuId(@NotNull(message = "skuId不能为空")
											   @PathVariable("skuId") Long skuId) {
		return AjaxResult.success(iMallSkuImagesService.list(new LambdaQueryWrapper<MallSkuImages>().eq(MallSkuImages::getSkuId,skuId)));
	}

    /**
     * 新增sku图片
     */
    @ApiOperation("新增sku图片")
    @PreAuthorize("@ss.hasPermi('mall:skuImages:add')")
    @Log(title = "sku图片", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallSkuImagesAddBo bo) {
        return toAjax(iMallSkuImagesService.insertByAddBo(bo) ? 1 : 0);
    }

	/**
	 * 新增sku图片
	 */
	@ApiOperation("新增sku图片")
	@Log(title = "sku图片", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping("/batchAdd")
	public AjaxResult<Void> batchAdd(@Validated @RequestBody List<MallSkuImages> bo) {
		return toAjax(iMallSkuImagesService.batchAdd(bo) ? 1 : 0);
	}

    /**
     * 修改sku图片
     */
    @ApiOperation("修改sku图片")
    @PreAuthorize("@ss.hasPermi('mall:skuImages:edit')")
    @Log(title = "sku图片", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallSkuImagesEditBo bo) {
        return toAjax(iMallSkuImagesService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除sku图片
     */
    @ApiOperation("删除sku图片")
    @PreAuthorize("@ss.hasPermi('mall:skuImages:remove')")
    @Log(title = "sku图片" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallSkuImagesService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 删除sku图片
	 */
	@ApiOperation("删除sku图片")
	@Log(title = "sku图片" , businessType = BusinessType.DELETE)
	@DeleteMapping("/removeByImgUrl/{imgUrl}")
	public AjaxResult<Void> removeByImgUrl(@NotEmpty(message = "imgUrl不能为空")
								   @PathVariable String imgUrl) {
		return toAjax(iMallSkuImagesService.remove(new LambdaQueryWrapper<MallSkuImages>().eq(MallSkuImages::getImgUrl,imgUrl)) ? 1 : 0);
	}
}
